 package client;

 import com.alibaba.druid.pool.DruidPooledConnection;
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import database.DatabaseConnection;
 import java.io.Serializable;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import tools.packet.BuddyListPacket;

 public class BuddyList
   implements Serializable
 {
   private static final long serialVersionUID = 1413738569L;
/*  21 */   private final Map<Integer, BuddylistEntry> buddies = new LinkedHashMap<>();
   private byte capacity;
   private boolean changed = false;

   @JsonCreator
   public BuddyList(@JsonProperty("capacity") byte capacity) {
/*  27 */     this.capacity = capacity;
   }

   public boolean contains(int characterId) {
/*  31 */     return this.buddies.containsKey(Integer.valueOf(characterId));
   }

   public boolean containsVisible(int characterId) {
/*  35 */     BuddylistEntry ble = this.buddies.get(Integer.valueOf(characterId));
/*  36 */     return (ble != null && ble.isVisible());
   }

   public byte getCapacity() {
/*  40 */     return this.capacity;
   }

   public void setCapacity(byte capacity) {
/*  44 */     this.capacity = capacity;
   }

   public BuddylistEntry get(int characterId) {
/*  48 */     return this.buddies.get(Integer.valueOf(characterId));
   }

   public BuddylistEntry get(String characterName) {
/*  52 */     String lowerCaseName = characterName.toLowerCase();
/*  53 */     for (BuddylistEntry ble : this.buddies.values()) {
/*  54 */       if (ble.getName().toLowerCase().equals(lowerCaseName)) {
/*  55 */         return ble;
       }
     }
/*  58 */     return null;
   }

   public void put(BuddylistEntry entry) {
/*  62 */     this.buddies.put(Integer.valueOf(entry.getCharacterId()), entry);
/*  63 */     this.changed = true;
   }

   public void remove(int characterId) {
/*  67 */     this.buddies.remove(Integer.valueOf(characterId));
/*  68 */     this.changed = true;
   }

   public Collection<BuddylistEntry> getBuddies() {
/*  72 */     return this.buddies.values();
   }

   public boolean isFull() {
/*  76 */     return (this.buddies.size() >= this.capacity);
   }

   public int[] getBuddyIds() {
/*  80 */     int[] buddyIds = new int[this.buddies.size()];
/*  81 */     int i = 0;
/*  82 */     for (BuddylistEntry ble : this.buddies.values()) {
/*  83 */       if (ble.isVisible()) {
/*  84 */         buddyIds[i++] = ble.getCharacterId();
       }
     }
/*  87 */     return buddyIds;
   }

   public void loadFromTransfer(List<CharacterNameAndId> data) {
/*  91 */     for (CharacterNameAndId qs : data) {
/*  92 */       put(new BuddylistEntry(qs.getName(), qs.getId(), qs.getGroup(), -1, qs.isVisible()));
     }
   }

   public void loadFromDb(int characterId) throws SQLException {
/*  97 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/*  98 */         PreparedStatement ps = con.prepareStatement("SELECT b.buddyid, b.pending, c.name AS buddyname, b.groupname FROM buddies AS b, characters AS c WHERE c.id = b.buddyid AND b.characterid = ?")) {
/*  99 */       ps.setInt(1, characterId);
/* 100 */       try (ResultSet rs = ps.executeQuery()) {

/* 102 */         while (rs.next()) {
/* 103 */           put(new BuddylistEntry(rs.getString("buddyname"), rs.getInt("buddyid"), rs.getString("groupname"), -1, (rs.getInt("pending") != 1)));
         }
       }
     }
   }


   public void addBuddyRequest(MapleClient c, int cidFrom, String nameFrom, int channelFrom, int levelFrom, int jobFrom) {
/* 111 */     put(new BuddylistEntry(nameFrom, cidFrom, "未指定群组", channelFrom, false));
/* 112 */     c.announce(BuddyListPacket.requestBuddylistAdd(cidFrom, nameFrom, channelFrom, levelFrom, jobFrom));
   }

   public void setChanged(boolean v) {
/* 116 */     this.changed = v;
   }

   public boolean changed() {
/* 120 */     return this.changed;
   }

   public enum BuddyOperation
   {
/* 125 */     添加好友, 删除好友;
   }

   public enum BuddyAddResult
   {
/* 130 */     好友列表已满, 已经是好友关系, 添加好友成功;
   }
 }


/* Location:              C:\PGX\Server.jar!\client\BuddyList.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */